package main

import (
	"bytes"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"os"
	"os/exec"
	"time"
)

func main() {
	r := gin.New()
	r.GET("/qrcode/:num", func(c *gin.Context) {
		c.Writer.Header().Set("Content-Type", "image/jpeg")
		number := c.Param("num")
		imgPath := "/Users/Harris/Desktop/wechat_write_path/" + number + ".jpeg"
		if _, err := os.Stat(imgPath); err == nil {
			// QRCode image exists
			QRCodeImgFile, _ := os.Open(imgPath)
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
		openCmd := "open"
		cmd := exec.Command(openCmd, "-n", "/Applications/WeChat.app", "--args", "processNum"+number)
		var out bytes.Buffer
		var stderr bytes.Buffer
		cmd.Stdout = &out
		cmd.Stderr = &stderr
		err := cmd.Run()
		if err != nil {
			fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
			return
		}
		time.Sleep(5 * time.Second)
		if _, err := os.Stat(imgPath); err == nil {
			QRCodeImgFile, err := os.Open(imgPath)
			if err != nil {
				fmt.Println("load image error", err)
			}
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
		time.Sleep(5 * time.Second)
		if _, err := os.Stat(imgPath); err == nil {
			QRCodeImgFile, err := os.Open(imgPath)
			if err != nil {
				fmt.Println("load image error", err)
			}
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
		time.Sleep(5 * time.Second)
		if _, err := os.Stat(imgPath); err == nil {
			QRCodeImgFile, err := os.Open(imgPath)
			if err != nil {
				fmt.Println("load image error", err)
			}
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
		time.Sleep(5 * time.Second)
		if _, err := os.Stat(imgPath); err == nil {
			QRCodeImgFile, err := os.Open(imgPath)
			if err != nil {
				fmt.Println("load image error", err)
			}
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
		time.Sleep(10 * time.Second)
		if _, err := os.Stat(imgPath); err == nil {
			QRCodeImgFile, err := os.Open(imgPath)
			if err != nil {
				fmt.Println("load image error", err)
			}
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
		time.Sleep(10 * time.Second)
		if _, err := os.Stat(imgPath); err == nil {
			QRCodeImgFile, err := os.Open(imgPath)
			if err != nil {
				fmt.Println("load image error", err)
			}
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
		time.Sleep(10 * time.Second)
		if _, err := os.Stat(imgPath); err == nil {
			QRCodeImgFile, err := os.Open(imgPath)
			if err != nil {
				fmt.Println("load image error", err)
			}
			defer QRCodeImgFile.Close()
			io.Copy(c.Writer, QRCodeImgFile)
			return
		}
	})

	r.GET("/deleteqrcode/:num", func(c *gin.Context) {
		number := c.Param("num")
		imgPath := "/Users/kidswant/Desktop/wechat_write_path/" + number + ".jpeg"
		openCmd := "rm"
		cmd := exec.Command(openCmd, imgPath)
		var out bytes.Buffer
		var stderr bytes.Buffer
		cmd.Stdout = &out
		cmd.Stderr = &stderr
		err := cmd.Run()
		if err != nil {
			c.JSON(200, gin.H{
				"message": fmt.Sprint(err) + ": " + stderr.String(),
			})
			return
		}
		c.JSON(200, gin.H{
			"message": "Delete the qrcode success",
		})
	})

	r.GET("/updatewechat", func(c *gin.Context) {
		shPath := "/Users/kidswant/Bot/update.sh"
		openCmd := "bash"
		cmd := exec.Command(openCmd, shPath)
		var out bytes.Buffer
		var stderr bytes.Buffer
		cmd.Stdout = &out
		cmd.Stderr = &stderr
		err := cmd.Run()
		if err != nil {
			c.JSON(200, gin.H{
				"message": fmt.Sprint(err) + ": " + stderr.String(),
			})
			return
		}
		c.JSON(200, gin.H{
			"message": "update wechat success",
		})

	})

	r.GET("/status", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"code": 1001,
			"msg":  "server is ok",
		})
	})

	r.Run(":3001")
}
